﻿using System;
using System.Collections.Generic;
using System.Text;

namespace AlgorithmTest
{
    // T_[四个数字排序]_[算法名]
    public class T_0041_MaxAreaOfIsland : IAlgorithm
    {
        // 695. 岛屿的最大面积

        // 给你一个大小为 m x n 的二进制矩阵 grid 。

        // 岛屿 是由一些相邻的 1 (代表土地) 构成的组合，这里的「相邻」要求两个 1 必须在 水平或者竖直的四个方向上 相邻。
        // 你可以假设 grid 的四个边缘都被 0（代表水）包围着。

        // 岛屿的面积是岛上值为 1 的单元格的数目。

        // 计算并返回 grid 中最大的岛屿面积。如果没有岛屿，则返回面积为 0 。


        public void Test()
        {
            // 算法参数定义
            var grid = new int[][]{
            new int[]{0,0,1,0,0,0,0,1,0,0,0,0,0},
            new int[]{0,0,0,0,0,0,0,1,1,1,0,0,0},
            new int[]{0,1,1,0,1,0,0,0,0,0,0,0,0},
            new int[]{0,1,0,0,1,1,0,0,1,0,1,0,0},
            new int[]{0,1,0,0,1,1,0,0,1,1,1,0,0},
            new int[]{0,0,0,0,0,0,0,0,0,0,1,0,0},
            new int[]{0,0,0,0,0,0,0,1,1,1,0,0,0},
            new int[]{0,0,0,0,0,0,0,1,1,0,0,0,0}};

            // 算法执行与打印
            Console.WriteLine(MaxAreaOfIsland(grid));
        }

        int[] di = { 0, 0, 1, -1 };
        int[] dj = { 1, -1, 0, 0 };
        // 算法
        // 深度优先搜索
        public int MaxAreaOfIsland(int[][] grid)
        {
            int ans = 0;
            for (int i = 0; i != grid.Length; ++i)
                for (int j = 0; j != grid[0].Length; ++j)
                    ans = Math.Max(ans, DFS(grid, i, j));
            return ans;
        }
        private int DFS(int[][] grid, int cur_i, int cur_j)
        {
            if (cur_i < 0 || cur_j < 0 || cur_i == grid.Length || cur_j == grid[0].Length || grid[cur_i][cur_j] != 1)
                return 0;
            grid[cur_i][cur_j] = 0;
            int[] di = { 0, 0, 1, -1 };
            int[] dj = { 1, -1, 0, 0 };
            int ans = 1;
            for (int index = 0; index != 4; ++index)
            {
                int next_i = cur_i + di[index], next_j = cur_j + dj[index];
                ans += DFS(grid, next_i, next_j);
            }
            return ans;
        }

        // 广度优先搜索
        public int MaxAreaOfIsland1(int[][] grid)
        {
            int ans = 0;
            for (int i = 0; i != grid.Length; ++i)
            {
                for (int j = 0; j != grid[0].Length; ++j)
                {
                    int cur = 0;
                    Queue<int> queuei = new Queue<int>();
                    Queue<int> queuej = new Queue<int>();
                    queuei.Enqueue(i);
                    queuej.Enqueue(j);
                    while (queuei.Count>0)
                    {
                        int cur_i = queuei.Dequeue(), cur_j = queuej.Dequeue();
                        if (cur_i < 0 || cur_j < 0 || cur_i == grid.Length || cur_j == grid[0].Length || grid[cur_i][cur_j] != 1)
                        {
                            continue;
                        }
                        ++cur;
                        grid[cur_i][cur_j] = 0;
                        int[] di = { 0, 0, 1, -1 };
                        int[] dj = { 1, -1, 0, 0 };
                        for (int index = 0; index != 4; ++index)
                        {
                            int next_i = cur_i + di[index], next_j = cur_j + dj[index];
                            queuei.Enqueue(next_i);
                            queuej.Enqueue(next_j);
                        }
                    }
                    ans = Math.Max(ans, cur);
                }
            }
            return ans;
        }
    }
}
